/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.manager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.betwixt.expression.Context;
import org.apache.commons.betwixt.strategy.ObjectStringConverter;

import com.genia.toolbox.basics.bean.RegisterableObjectStringConverter;

/**
 * {@link ObjectStringConverter} that can be dynamically defined.
 */
@SuppressWarnings("serial")
public class AggregatorObjectStringConverter
    extends ObjectStringConverter
{
  /**
   * the registered binding strategies.
   */
  private List<RegisterableObjectStringConverter> objectStringConverters = new ArrayList<RegisterableObjectStringConverter>();

  /**
   * cached values for {@link BindingType}.
   */
  private final Map<Class<?>, RegisterableObjectStringConverter> objectStringConvertersByType = new HashMap<Class<?>, RegisterableObjectStringConverter>();



  /**
   * getter for the objectStringConverters property.
   * 
   * @return the objectStringConverters
   */
  public List<RegisterableObjectStringConverter> getBindingStrategies()
  {
    return objectStringConverters;
  }



  /**
   * setter for the objectStringConverters property.
   * 
   * @param objectStringConverters
   *          the objectStringConverters to set
   */
  public void setBindingStrategies(List<RegisterableObjectStringConverter> objectStringConverters)
  {
    this.objectStringConverters = objectStringConverters;
  }



  /**
   * returns the {@link RegisterableObjectStringConverter} associated to the
   * given type, or <code>null</code> if no such element is registered.
   * 
   * @param type
   *          the type to get a {@link RegisterableObjectStringConverter} for
   * @return the {@link RegisterableObjectStringConverter} associated to the
   *         given type, or <code>null</code> if no such element is registered
   */
  private RegisterableObjectStringConverter getRegisterableObjectStringConverter(Class<?> type)
  {
    if (type == null) {
      return null;
    }
    if (!objectStringConvertersByType.containsKey(type)) {
      RegisterableObjectStringConverter res = null;
      for (RegisterableObjectStringConverter registerableObjectStringConverter : objectStringConverters) {
        if (registerableObjectStringConverter.getType().isAssignableFrom(type)) {
          res = registerableObjectStringConverter;
          break;
        }
      }
      objectStringConvertersByType.put(type, res);
    }
    return objectStringConvertersByType.get(type);
  }



  /**
   * Converts an object to a string representation. This basic implementation
   * returns object.toString() or an empty string if the given object is null.
   * 
   * @param object
   *          the object to be converted, possibly null
   * @param type
   *          the property class of the object, not null
   * @param context
   *          the context, not null
   * @return a String representation, not null
   * @see org.apache.commons.betwixt.strategy.ObjectStringConverter#objectToString(Object,
   *      Class, Context)
   */
  @Override
  public String objectToString(Object object, @SuppressWarnings("unchecked")
  Class type, Context context)
  {
    if (object != null) {
      RegisterableObjectStringConverter registerableObjectStringConverter = getRegisterableObjectStringConverter(type);
      if (registerableObjectStringConverter != null) {
        if (object.getClass().isAssignableFrom(type)) {
          return registerableObjectStringConverter.objectToString(object, type, context);
        }
        if (context.getBean().getClass().isAssignableFrom(type)) {
          return registerableObjectStringConverter.objectToString(context.getBean(), type, context);
        }
      }
    }
    return super.objectToString(object, type, context);
  }



  /**
   * Converts a string representation to an object. It is acceptable for an
   * implementation to return the string if it cannot convert the string to the
   * given class type. This basic implementation just returns a string.
   * 
   * @param value
   *          the String to be converted
   * @param type
   *          the property class to be returned (if possible), not null
   * @param context
   *          the context, not null
   * @return an Object converted from the String, not null
   * @see org.apache.commons.betwixt.strategy.ObjectStringConverter#stringToObject(String,
   *      Class, Context)
   */

  @Override
  public Object stringToObject(String value, @SuppressWarnings("unchecked")
  Class type, Context context)
  {
    RegisterableObjectStringConverter registerableObjectStringConverter = getRegisterableObjectStringConverter(type);
    if (registerableObjectStringConverter != null) {
      return registerableObjectStringConverter.stringToObject(value, type, context);
    }
    return super.stringToObject(value, type, context);

  }

}
